﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using System.IO;
using System.Data.SqlClient;
using System.Drawing.Imaging;

namespace QuanLyHangXeKhach
{
    public partial class frmcnnhanvienphutrachxe : UserControl
    {
        CapNhatNhanVienPhuTrachXe cnnvpt = new CapNhatNhanVienPhuTrachXe();
        DieuKhien dk = new DieuKhien();
        string imagename;//lưu tên của hình
        public frmcnnhanvienphutrachxe()
        {
            InitializeComponent();
        }

        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcnnvptx;
                btnthemxe.Command = newValue.showcnxe;
            }
            else
            {
                btndong.Command = null;
                btnthemxe.Command = null;
            }
        }

        #endregion

        private void frmcnnhanvienphutrachxe_Load(object sender, EventArgs e)
        {
            txttimkiem.WatermarkText = "Tìm kiếm theo mã hoặc họ tên";

            cobchucvu.Items.Add("Tài xế");
            cobchucvu.Items.Add("Phục vụ");

            cobgioitinh.Items.Add("Nam");
            cobgioitinh.Items.Add("Nữ");
            cobgioitinh.Items.Add("Khác");

            cobsoxe.DataSource = cnnvpt.layxe();
            cobsoxe.DisplayMember = "bienxe";
            cobsoxe.ValueMember = "maxe";
            cobsoxe.Text = "";  

            Loadfrm();
        
        }

        private void Loadfrm()
        {          

            List<nhanvienphutrachxe> nvpt = cnnvpt.laydsnhanvienpt();
            advTree1.Nodes.Clear();
            foreach (var row in nvpt)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.manv, row.hoten, row.gioitinh, row.ngaysinh, row.diachi, row.soCMND, row.sodt, row.chucvu, row.xe));
            }
        }

        private Node CreatenoidenRow(string ma, string ten, string giotinh, string ngay, string diachi, string cmnd, string sdt, string chucvu, string xe)
        {
            Node node = new Node(ma);
            node.Cells.Add(new Cell(ten));
            node.Cells.Add(new Cell(giotinh));
            node.Cells.Add(new Cell(ngay));
            node.Cells.Add(new Cell(diachi));
            node.Cells.Add(new Cell(cmnd));
            node.Cells.Add(new Cell(sdt));
            node.Cells.Add(new Cell(chucvu));
            node.Cells.Add(new Cell(xe));
            return node;
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                txtmanv.Text = node.Cells[0].Text;
                txthoten.Text = node.Cells[1].Text;
                cobgioitinh.Text = node.Cells[2].Text;
                maskngaysinh.Text = node.Cells[3].Text.Replace("/","");
                txtdiachi.Text = node.Cells[4].Text;
                cobchucvu.Text = node.Cells[7].Text;
                cobsoxe.Text = node.Cells[8].Text;
                txtCMND.Text = node.Cells[5].Text;
                txtsdt.Text =  node.Cells[6].Text;

                Image img = Image.FromStream(cnnvpt.layhinhnvpt(txtmanv.Text));
                if (img == null) return;
                picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                picanhdaidien.Image = img;
            }
            catch (Exception) { }
        }

        public void xoatrang()
        {
            txtCMND.Text = "";
            txtdiachi.Text = "";
            txthoten.Text = "";
            txtsdt.Text = "";
            txttimkiem.Text = "";
            cobchucvu.Text = "";
            cobgioitinh.Text = "";
            cobsoxe.Text = "";
            maskngaysinh.Text = "";
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Text = "Lưu";
            btnxoa.Text = "Hủy bỏ";
            btnthem.Enabled = false;
            xoatrang();
            txtmanv.Text = dk.NextID( cnnvpt.manvptlonnhat().ToString(), "NVX");           
            this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (maskngaysinh.Text == "" || txthoten.Text == "" || cobgioitinh.Text == "" || txtdiachi.Text == "" || txtCMND.Text == "" || cobchucvu.Text == "" || cobsoxe.Text == "")
                MessageBoxEx.Show("Nhập đủ thông tin trước khi tiếp tục ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if(dk.catngay(maskngaysinh.Text) > System.DateTime.Now)
                MessageBoxEx.Show("Ngày sinh nhập vào không hợp lệ ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (btnsua.Text == "Lưu")
                {
                    MemoryStream stream = new MemoryStream();
                    picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                    cnnvpt.themnvpt(txtmanv.Text, txthoten.Text, cobgioitinh.Text, dk.catngay(maskngaysinh.Text), txtdiachi.Text, txtsdt.Text, txtCMND.Text, stream, cobchucvu.Text, cobsoxe.SelectedValue.ToString());
                    MessageBoxEx.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    xoatrang();
                    txtmanv.Text = dk.NextID(cnnvpt.manvptlonnhat().ToString(), "NVX");
                    Loadfrm();

                }
                else
                {
                    MemoryStream stream = new MemoryStream();
                    picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                    cnnvpt.suanvpt(txtmanv.Text, txthoten.Text, cobgioitinh.Text, dk.catngay(maskngaysinh.Text), txtdiachi.Text, txtsdt.Text, txtCMND.Text, stream, cobchucvu.Text, cobsoxe.SelectedValue.ToString());
                    MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadfrm();
                }
            }
        }

        private void btnduyetdanh_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog dldlg = new OpenFileDialog();
                dldlg.InitialDirectory = @":d\";
                dldlg.Filter = "Image File(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (dldlg.ShowDialog() == DialogResult.OK)
                {
                    imagename = dldlg.FileName;
                    Bitmap newimg = new Bitmap(imagename);
                    picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                    picanhdaidien.Image = (Image)newimg;
                }
                dldlg = null;

            }
            catch (System.ArgumentException ae)
            {
                imagename = " ";
                MessageBox.Show(ae.Message.ToString());

            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnxoa.Text == "Hủy bỏ")
            {
                btnthem.Enabled = true;
                btnsua.Text = "Sửa";
                btnxoa.Text = "Xóa";
                xoatrang();
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txtmanv.Text == "")
                    MessageBoxEx.Show("Chọn một đối tượng cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    DialogResult dr = MessageBoxEx.Show("Bạn muốn xóa nhân viên " + txthoten.Text + " này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (dr == DialogResult.Yes)
                    {
                        cnnvpt.xoanvpt(txtmanv.Text);
                        MessageBoxEx.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xoatrang();
                        txtmanv.Text = "";
                        Loadfrm();
                    }
                }
            }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            List<nhanvienphutrachxe> nvpt = cnnvpt.laydsnhanvienpt(txttimkiem.Text);
            advTree1.Nodes.Clear();
            foreach (var row in nvpt)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.manv, row.hoten, row.gioitinh, row.ngaysinh, row.diachi, row.soCMND, row.sodt, row.chucvu, row.xe));
            }
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void maskngaysinh_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void maskngaysinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
